package org.edu.service;

import java.io.File;

import java.io.IOException;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import org.apache.log4j.Logger;

import org.edu.common.uiltst.RWxls;
import org.edu.dao.DeptDao;
import org.edu.dao.TreeNodeDao;
import org.edu.model.Dept;
import org.edu.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = { RuntimeException.class }, propagation = Propagation.REQUIRED)
public class DeptService extends ServiceAdapter<Dept, Integer> {
	@Autowired
	private DeptDao deptDao;
	@Autowired
	private TreeNodeDao treeNodeDao;
	private static final Logger logger = Logger.getLogger(DeptService.class);

	@Override
	public List<Dept> list() {
		// TODO Auto-generated method stub
		// System.out.println("service调用");

		return deptDao.loadAll();
	}

	@Override
	public void update(Dept object) {
		// TODO Auto-generated method stub
		// 同时修改 树结构 的name
		TreeNode t = treeNodeDao.get(object.getDeptno());
		t.setName(object.getDeptname());
		treeNodeDao.update(t);
		deptDao.update(object);

	}

	@Override
	public void save(Dept object) {
		// TODO Auto-generated method stub
		deptDao.save(object);
	}

	@Override
	public void delete(Integer id) {
		// TODO Auto-generated method stub
		deptDao.delete(deptDao.get(id));
	}

	@Override
	public Dept show(Integer id) {
		// TODO Auto-generated method stub
		return deptDao.get(id);
	}

	/**
	 * 从xls 文件写入 到数据库
	 * 
	 * RWxls.readXls(
	 * "G:\\java3-Job\\HospitalM\\src\\main\\webapp\\xls\\部门信息表.xls",1);
	 * 
	 * @throws IOException
	 */
	public void savexlsWdataUpload(File f) throws IOException {
		List<String[]> list = RWxls.readXls(f, 0);
		List<Dept> dlist = new ArrayList<Dept>();
		List<TreeNode> tlist = new ArrayList<TreeNode>();

		for (int i = 0; i < list.size(); i++) {
			String[] deptString = Arrays.copyOfRange(list.get(i), 0, 5);
			Dept d = new Dept(deptString);
			TreeNode t = new TreeNode();

			t.setId(d.getDeptno());
			t.setName(d.getDeptname());
			t.setPid(Integer.parseInt(list.get(i)[5]));

			d.setTreenode(t);

			tlist.add(t);
			dlist.add(d);
			// 删除 全部表数据 在进行插入， 问题：中间失败了 怎么回滚
		}

		deptDao.deleteAll(deptDao.loadAll());// 清空表
		logger.info("已清空表》》》》》》》》》");
		for (int j = 0; j < tlist.size(); j++) {
			logger.info("正在更新第" + j + "条");
			treeNodeDao.save(tlist.get(j));
			deptDao.save(dlist.get(j));

		}
		// treeNodeDao.save(tlist.get(10));
		// deptDao.save(dlist.get(10));
		// if(true) throw new RuntimeException();

	}

	

	/**
	 * 从数据库 写入到xls 使用jxj 
	 * 因为使用poi 的话在文件步存在的情况下  会报错jxj 则不会
	 * 
	 */
	public  void datawritxls(File f){
		WritableWorkbook wwb = null;
		try {// 首先要使用Workbook类的工厂方法创建一个可写入的工作薄(Workbook)对象
			wwb = Workbook.createWorkbook(f);
		} catch (IOException e) {
			e.printStackTrace();
		}
		logger.info("准备写入文件：" );
		List<Dept> dl = deptDao.loadAll();
		if (wwb != null) {
			WritableSheet ws = wwb.createSheet("sheet1", 0);
			//设置标题
			Label labelC1 = new Label(0, 0,"部门编号");
			Label labelC2 = new Label(1, 0, "部门名称");
			Label labelC3 = new Label(2, 0, "助记符");
			Label labelC4 = new Label(3, 0, "负责人");
			Label labelC5 = new Label(4, 0, "部门属性");
			Label labelC6 = new Label(5, 0, "父项");
			
			try {
				logger.info("正在写入标题");
				ws.addCell(labelC1);
				ws.addCell(labelC2);
				ws.addCell(labelC3);
				ws.addCell(labelC4);
				ws.addCell(labelC5);
				ws.addCell(labelC6);
			} catch (RowsExceededException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (WriteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			for (int i = 0; i < dl.size(); i++) {
				labelC1 = new Label(0, i+1, dl.get(i).getDeptno()+"");
				labelC2 = new Label(1, i+1, dl.get(i).getDeptname());
				labelC3 = new Label(2, i+1, dl.get(i).getDeptabb());
				labelC4 = new Label(3, i+1, dl.get(i).getDeptpic());
				labelC5 = new Label(4, i+1, dl.get(i).getDeptp());
				labelC6 = new Label(5, i+1, treeNodeDao.get(dl.get(i).getDeptno()).getPid()+"");
				
				try {
					logger.info("正在写入第" + i + "条");
					ws.addCell(labelC1);
					ws.addCell(labelC2);
					ws.addCell(labelC3);
					ws.addCell(labelC4);
					ws.addCell(labelC5);
					ws.addCell(labelC6);
				} catch (RowsExceededException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (WriteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			try {
				// 从内存中写入文件中
				wwb.write();
				// 关闭资源，释放内存
				wwb.close();
				logger.info("写入完成");
			} catch (IOException e) {
				e.printStackTrace();
			} catch (WriteException e) {
				e.printStackTrace();
			}
		}
		
	}
	public void setHeader() {

	}

	/**
	 * 用于测试事务的回滚
	 * 
	 * @throws IOException
	 */

	public void updatatest() throws IOException {

		deptDao.deleteAll(deptDao.loadAll());// 清空表
		if (true)
			throw new IOException();

	}

}
